<template>
  <el-container class="container">
    <!-- 头部区 -->
    <el-header>
      <div>
        <i>StudyingPlatform</i>
        <i class="el-icon-alarm-clock"></i>
        <span>sql知识学习交流平台</span>
      </div>
      <el-button type="info" @click="quit" :icon="SwitchButton">退出</el-button>
      <el-button type="primary" @click="toHome">到用户端</el-button>
    </el-header>
    <!-- 页面主体区 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div
          class="toggle-button"
          :class="iconChange"
          @click="toggleCollapse"
          title="折叠展开"
        >
          <el-icon v-if="isCollapse"><Expand /></el-icon>
          <el-icon v-else><Fold /></el-icon>
        </div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 菜单 -->
          <el-menu-item
            :index="item.path"
            class="menu-item"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 图标 -->
            <el-icon v-if="item.icon === 'HomeFilled'"><HomeFilled /></el-icon>
            <el-icon v-if="item.icon === 'UserFilled'"><UserFilled /></el-icon>
            <el-icon v-if="item.icon === 'Paperclip'"><Paperclip /></el-icon>
            <el-icon v-if="item.icon === 'Document'"><Document /></el-icon>
            <el-icon v-if="item.icon === 'Fries'"><Fries /></el-icon>
            <!-- 文本 -->
            <span slot="title">{{ item.title }}</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 内容主体区 -->
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance, ComponentInternalInstance, reactive, onMounted, watch } from 'vue';
import { Fold, Expand, SwitchButton,HomeFilled, UserFilled, Paperclip, Document, Fries } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router';

const { appContext } = getCurrentInstance() as ComponentInternalInstance;
const proxy = appContext.config.globalProperties;
const router = useRouter();

// 用户角色权限
const userRoleId = ref(proxy.$storage.getItem('userInfo').userRoleId || '');
// 左侧菜单
const menuList = reactive([
  {
    id: '1',
    icon: 'HomeFilled',
    title: '首页',
    path: '/manager/home',
  },
  {
    id: '2',
    icon: 'UserFilled',
    title: '用户管理',
    path: '/manager/users',
  },
  {
    id: '3',
    icon: 'Paperclip',
    title: '标签管理',
    path: '/manager/tags',
  },
  {
    id: '4',
    icon: 'Document',
    title: '帖子管理',
    path: '/manager/posts',
  },
  {
    id: '5',
    icon: 'Fries',
    title: '关卡管理',
    path: '/manager/levels'
  }
]);
// 是否折叠
const isCollapse = ref(false);
// 改变折叠字体图标
const iconChange = ref('el-icon-s-fold');
// 当前状态路由
const activePath = ref('/' + proxy.$storage.getItem('activePath') || '/manager/home');

onMounted(() => {
  if (userRoleId.value === 2) {
    menuList.splice(1, 1);
  }
});

// 监听当前路由
watch(() => router.currentRoute.value,
  (newValue: any) => {
    proxy.$storage.setItem(
      'activePath',
      proxy.$route.path.substring(1, newValue.path.length)
    );
    activePath.value = '/' + proxy.$storage.getItem('activePath');
  },
  { immediate: true }
);

// 退出
function quit() {
  ElMessageBox.confirm('此操作将退出登录, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      // 清除浏览器缓存
      proxy.$storage.clearAll();
      // 跳转到登录页面
      proxy.$router.push('/login');
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消操作',
        duration: 1000,
      });
    });
};
// 切换菜单折叠与展开
function toggleCollapse() {
  isCollapse.value = !isCollapse.value;
  if (isCollapse.value) {
    iconChange.value = 'el-icon-s-unfold';
  } else {
    iconChange.value = 'el-icon-s-fold';
  }
};
// 跳转到用户端
function toHome() {
  proxy.$storage.setItem('activePath', 'home');
  proxy.$router.push('/home');
};
</script>

<style lang="less" scoped>
.el-container {
  height: 100px;
}

.container {
  height: 100%;
  width: 100vw;

  .el-header {
    background-color: #373b41;
    //display: flex;
    //justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;

    div {
      width: 360px;
      height: 50px;
      margin: 5px 0;
      float: left;
      display: flex;
      align-items: center;

      i:nth-child(1) {
        font-family: '华文隶书';
        padding-left: 15px;
        margin: 0 5px;
        font-size: 20px;
      }

      i:nth-child(2) {
        font-size: 24px;
      }

      span {
        margin-left: 15px;
        display: inline-block;
        font-size: 20px;
        height: 60px;
        line-height: 58px;
      }
    }

    .el-button {
      float: right;
      margin: 10px;
    }
  }

  .el-aside {
    background-color: #333744;
    transition: all 0.5s;

    .iconfont {
      margin-right: 10px;
    }

    .toggle-button {
      background-color: #4a5064;
      font-size: 24px;
      width: 100%;
      height: 36px;
      line-height: 36px;
      color: #fff;
      text-align: center;
      cursor: pointer;
    }

    .el-menu {
      border-right: none;
    }

    .el-menu-item {
      background-color: #1f2d3d !important;

      &:hover {
        background-color: #001528 !important;
      }
    }

    .el-menu .menu-item {
      background-color: #333744 !important;
    }

    .el-menu .menu-item:hover {
      background-color: #292c36 !important;
    }
  }

  .el-main {
    background-color: #eaedf1;
    padding: 0 0 60px;
  }
}
</style>
